package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.RSourceCorrelation;
import com.jilefojing.beans.query.RSourceCorrelationQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRSourceCorrelationDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRSourceCorrelationMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月26日 10时33分29秒
 */
@Repository
public class RSourceCorrelationDao extends BaseDao<RSourceCorrelation> implements IRSourceCorrelationDao {
	@Autowired
	private IRSourceCorrelationMapper rSourceCorrelationMapper;

	@Override
	public Pagination<RSourceCorrelation> getPagination(RSourceCorrelationQuery rSourceCorrelationQuery, Pagination<RSourceCorrelation> pagination) {
		Example example = new Example(RSourceCorrelation.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(rSourceCorrelationQuery.getIds())) {
			criteria.andIn(RSourceCorrelation.FIELD_ID, rSourceCorrelationQuery.getIds());
		}

		if (Detect.notEmpty(rSourceCorrelationQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemType : rSourceCorrelationQuery.getItemTypes()) {
				itemTypeCodes.add(_ItemType.getCode());
			}
			criteria.andIn(RSourceCorrelation.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(rSourceCorrelationQuery.getItemIds())) {
			criteria.andIn(RSourceCorrelation.FIELD_ITEMID, rSourceCorrelationQuery.getItemIds());
		}
		if (Detect.notEmpty(rSourceCorrelationQuery.getRelationTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemType : rSourceCorrelationQuery.getRelationTypes()) {
				itemTypeCodes.add(_ItemType.getCode());
			}
			criteria.andIn(RSourceCorrelation.FIELD_RELATIONTYPE, itemTypeCodes);
		}
		if (Detect.notEmpty(rSourceCorrelationQuery.getRelationIds())) {
			criteria.andIn(RSourceCorrelation.FIELD_RELATIONID, rSourceCorrelationQuery.getRelationIds());
		}
		if (Detect.notEmpty(rSourceCorrelationQuery.getSorts())) {
			criteria.andIn(RSourceCorrelation.FIELD_SORT, rSourceCorrelationQuery.getSorts());
		}
		if (Detect.notEmpty(rSourceCorrelationQuery.getCreateUserIds())) {
			criteria.andIn(RSourceCorrelation.FIELD_CREATEUSER, rSourceCorrelationQuery.getCreateUserIds());
		}

		if (rSourceCorrelationQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(RSourceCorrelation.FIELD_CREATETIME, rSourceCorrelationQuery.getEqualCreateTime());
		}

		if (rSourceCorrelationQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(RSourceCorrelation.FIELD_CREATETIME, rSourceCorrelationQuery.getEqualAndBeforCreateTime());
		}

		if (rSourceCorrelationQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(RSourceCorrelation.FIELD_CREATETIME, rSourceCorrelationQuery.getEqualAndAfterCreateTime());
		}

		if (rSourceCorrelationQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(RSourceCorrelation.FIELD_CREATETIME, rSourceCorrelationQuery.getBeforCreateTime());
		}

		if (rSourceCorrelationQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(RSourceCorrelation.FIELD_CREATETIME, rSourceCorrelationQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}
}
